<?php
  
include 'checkSession.php';


$lat = 26.927799;
$lon = -82.055199;
  
  $rad = 1.5;  // radius of bounding circle in kilometers
  
  $R = 6371;  // earth's radius, km
  
  // first-cut bounding box (in degrees)
  $maxLat = $lat + rad2deg($rad/$R);
  $minLat = $lat - rad2deg($rad/$R);
  // compensate for degrees longitude getting smaller with increasing latitude
  $maxLon = $lon + rad2deg($rad/$R/cos(deg2rad($lat)));
  $minLon = $lon - rad2deg($rad/$R/cos(deg2rad($lat)));
  
  // convert origin of filter circle to radians
  $lat = deg2rad($lat);
  $lon = deg2rad($lon);

 
$query="Select sp_id,sp_latitude,sp_longitude, 
       acos(sin($lat)*sin(radians(sp_latitude)) + cos($lat)*cos(radians(sp_latitude))*cos(radians(sp_longitude)-$lon))*$R As D
From (
  Select sp_id,sp_latitude,sp_longitude
  From Sign_Posts
  Where sp_latitude>$minLat And sp_latitude<$maxLat
    And sp_longitude>$minLon And sp_longitude<$maxLon
  ) As FirstCut 
Where acos(sin($lat)*sin(radians(sp_latitude)) + cos($lat)*cos(radians(sp_latitude))*cos(radians(sp_longitude)-$lon))*$R < $rad
Order By D";
echo $query;
  
$res = mysql_query($query);
?>

<html>
<body>
<table>
<? foreach ($points as $point): ?>
  <tr>
    <td><?= $point->Postcode ?></td>
    <td><?= number_format($point->D,1) ?></td>
    <td><?= number_format($point->Lat,3) ?></td>
    <td><?= number_format($point->Lon,3) ?></td>
  </tr>
<? endforeach ?>
</table>
</body>
</html>